using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace Commonplace
{
	/// <summary>
	/// Summary description for ImageThumbnail.
	/// </summary>
	public class ImageThumbnail : System.Windows.Forms.UserControl
	{
		private System.Windows.Forms.PictureBox _picture;
		private System.Windows.Forms.Panel _imgPanel;
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public override Cursor Cursor
		{
			get
			{
				return base.Cursor;
			}
			set
			{
				_imgPanel.Cursor = value;
				_picture.Cursor = value;
			}
		}


		public Image Image
		{
			get { return _picture.Image; }
			set { 

				//calculate proper size

				/*
				 * if image width > image height,
				 *     scale width to box
				 *     scale height to aspect ratio times width
				 */

				Image img = value;
				_picture.Image = img;

				ResizeImagePanel();

			}
		}

		public ImageThumbnail()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this._picture = new System.Windows.Forms.PictureBox();
			this._imgPanel = new System.Windows.Forms.Panel();
			this._imgPanel.SuspendLayout();
			this.SuspendLayout();
			// 
			// _picture
			// 
			this._picture.Dock = System.Windows.Forms.DockStyle.Fill;
			this._picture.Location = new System.Drawing.Point(2, 2);
			this._picture.Name = "_picture";
			this._picture.Size = new System.Drawing.Size(108, 132);
			this._picture.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
			this._picture.TabIndex = 0;
			this._picture.TabStop = false;
			this._picture.Click += new System.EventHandler(this._picture_Click);
			this._picture.MouseEnter += new System.EventHandler(this._picture_MouseEnter);
			this._picture.MouseLeave += new System.EventHandler(this._picture_MouseLeave);
			// 
			// _imgPanel
			// 
			this._imgPanel.Controls.Add(this._picture);
			this._imgPanel.DockPadding.All = 2;
			this._imgPanel.Location = new System.Drawing.Point(40, 16);
			this._imgPanel.Name = "_imgPanel";
			this._imgPanel.Size = new System.Drawing.Size(112, 136);
			this._imgPanel.TabIndex = 1;
			this._imgPanel.Click += new System.EventHandler(this._imgPanel_Click);
			// 
			// ImageThumbnail
			// 
			this.Controls.Add(this._imgPanel);
			this.Name = "ImageThumbnail";
			this.Size = new System.Drawing.Size(200, 168);
			this._imgPanel.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		protected override void OnResize(EventArgs e)
		{
			base.OnResize (e);

			ResizeImagePanel();
		}


		private void ResizeImagePanel()
		{
			Image img = _picture.Image;

			if (null == img)
				return;

			float imageAspectRatio = (float) img.Height / (float) img.Width;
			float panelAspectRatio = (float) Height / (float) Width;
			bool useWidth = true;

			if (img.Height > img.Width)
			{
				useWidth = false;
				if (panelAspectRatio > imageAspectRatio)
					useWidth = true;
			}
			else
			{
				if (imageAspectRatio > panelAspectRatio)
					useWidth = false;
			}

			int thumbHeight = 0;
			int thumbWidth = 0;

			if (true == useWidth)
			{
				thumbWidth = this.Width;
				float aspectRatio = (float) img.Height / (float) img.Width;
				thumbHeight = (int) (thumbWidth * aspectRatio);
				_imgPanel.Height = thumbHeight;
				_imgPanel.Width = thumbWidth;
				_imgPanel.Left = 0;
				_imgPanel.Top = (this.Height - thumbHeight) / 2;
			}
			else
			{
				thumbHeight = this.Height;
				float aspectRatio = (float) img.Height / (float) img.Width;
				thumbWidth = (int) (thumbHeight / aspectRatio);
				_imgPanel.Height = thumbHeight;
				_imgPanel.Width = thumbWidth;
				_imgPanel.Top = 0;
				_imgPanel.Left = (this.Width - thumbWidth) / 2;
			}
				
		}

		private void _picture_MouseEnter(object sender, System.EventArgs e)
		{
			_imgPanel.BackColor = SystemColors.HotTrack;		
		}

		private void _picture_MouseLeave(object sender, System.EventArgs e)
		{
			_imgPanel.BackColor = SystemColors.Control;
		}

		private void _imgPanel_Click(object sender, System.EventArgs e)
		{
			OnClick(e);		
		}

		private void _picture_Click(object sender, System.EventArgs e)
		{
			OnClick(e);
		}
	}
}
